import { computed, ref, unref, watchEffect } from 'vue'
import dayjs from 'dayjs'
import { reformatDatetime } from '@/utils/index.js'

/**
 * 订单相关
 */
export const useOrder = (order) => {
	const orderInfo = ref(order)
	
	/**
	 * 阶梯价格列表排序
	 */
	const sortStepPrices = (activeStepPrices) => {
		if (activeStepPrices && activeStepPrices.length) {
			return activeStepPrices.sort((a, b) => {
				return a.enrollNum - b.enrollNum
			})
		} else {
			return []
		}
	}
	
	const updateOrderInfo = (detail) => {
		orderInfo.value = detail
	}
	
	watchEffect(() => {
		if (orderInfo.value) {
			if (orderInfo.value.activeStepPrices) {
				sortStepPrices(orderInfo.value.activeStepPrices)
			}
			if (orderInfo.value.content && !orderInfo.value.activeContents) {
				try {
					orderInfo.value.activeContents = JSON.parse(orderInfo.value.content)
				} catch(e) {
					orderInfo.value.activeContents = [{
						type: 1,
						content: orderInfo.value.content
					}]
				}
			}
		}
	})
	
	/**
	 * 是否报名活动订单
	 * orderType 订单类型(1-团购订单,2-报名订单)
	 */
	const isSignUp = computed(() => {
		return orderInfo.value && (orderInfo.value.orderType == 2 || orderInfo.value.orderType == 4)
	})
	
	// 活动图片
	const photos = computed(() => {
		return orderInfo.value && orderInfo.value.activeImg && orderInfo.value.activeImg.split(',') || []
	})
	
	// 活动封面
	const coverPicture = computed(() => {
		return photos.value.length ? photos.value[0] : ''
	})
	
	/**
	 * 是否待支付状态
	 * orderState 订单状态(1-待支付,2-待核销,3-核销中,4-已取消,5-已完成,6-已关闭,8-支付中)
	 */
	const isWaitPay = computed(() => {
		return orderInfo.value && (orderInfo.value.orderState === 1 || orderInfo.value.orderState === 8)
	})
	
	// 待支付剩余时间, 支付等待时间默认为15分钟
	const remainPayTime = () => {
		if (isWaitPay.value) {
			const end = dayjs(reformatDatetime(orderInfo.value.orderTime)).add(15, 'minute')
			const now = dayjs()
			let sec = end.diff(now, 'second')
			if (sec < 0) {
				return 0
			}
			const min = parseInt(sec / 60, 10) || 0
			// console.log('支付剩余时间：', sec, min, parseInt(sec / 60, 10))
			sec = sec % 60
			return `00:${min < 10 ? '0' + min : min}:${sec < 10 ? '0'+sec : sec}`
		}
		return 0
	}
	
	// 活动结束剩余时间
	const remainEndTime = () => {
		const end = dayjs(reformatDatetime(orderInfo.value.endTime))
		const now = dayjs()
		let day = end.diff(now, 'day')
		if (day < 0) {
			day = 0
		}
		let hour = end.diff(now, 'hour')
		if (hour >= 24) {
			hour = hour % 24
		}
		let min = end.diff(now, 'minute')
		if (min >= 60) {
			min = min % 60
		} else if (min < 0) {
			min = 0
		}
		let sec = end.diff(now, 'second')
		if (sec >= 60) {
			sec = sec % 60
		} else if (sec < 0) {
			sec = 0
		}
		const isEnd = day === 0 && hour === 0 && min === 0 && sec === 0
		return {
			day,
			hour,
			min,
			sec,
			isEnd
		}
	}
	
	// 最少成团人数，如果是阶梯人数，为最小阶梯报名人数
	const minGroupNum = computed(() => {
		if (orderInfo.value) {
			if (orderInfo.value.stepPriceState === 2 && orderInfo.value.activeStepPrices && orderInfo.value.activeStepPrices.length) {
				return orderInfo.value.activeStepPrices[0].enrollNum
			} else {
				return orderInfo.value.enrollNum
			}
		}
	})
	
	return {
		isSignUp,
		photos,
		coverPicture,
		remainPayTime,
		remainEndTime,
		minGroupNum,
		orderInfo,
		updateOrderInfo
	}
}